{extend name="main" /}

{block name="title_seo"}
<title>找回密码</title>
<meta name="keywords" charset="UTF-8" content="">
<meta name="description" content="">
{/block}

{block name="body"}
<div class="layui-container">
    <div class="layui-row layui-col-space30" style="padding: 150px 0;text-align: center; margin-top: 60px;" >
        <style>
            .demo-login-container{width: 320px; margin: 21px auto 0;}
            .demo-login-other .layui-icon{position: relative; display: inline-block; margin: 0 2px; top: 2px; font-size: 26px;}
        </style>
        <form class="layui-form">
            <div class="demo-login-container">
                <div class="page-container">
                    <div class="layui-form-item">
                        <div class="layui-input-wrap">
                            <div class="layui-input-prefix">
                                <i class="layui-icon layui-icon-cellphone"></i>
                            </div>
                            <input type="text" name="mobile" id="mobile" value="" lay-verify="required|phone" placeholder="手机号"
                                   lay-reqtext="请填写手机号" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-row">
                            <div class="layui-col-xs7">
                                <div class="layui-input-wrap">
                                    <div class="layui-input-prefix">
                                        <i class="layui-icon layui-icon-vercode"></i>
                                    </div>
                                    <input type="text" name="code" value="" lay-verify="required" placeholder="验证码"
                                           lay-reqtext="请填写验证码" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-col-xs5">
                                <div style="margin-left: 11px;">
                                    <button type="button" class="layui-btn layui-btn-fluid layui-btn-primary" id="get-vercode" onclick="sendSmsCode()">
                                        获取验证码
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-wrap">
                            <div class="layui-input-prefix">
                                <i class="layui-icon layui-icon-password"></i>
                            </div>
                            <input type="password" name="password" value="" lay-verify="required" placeholder="设置新密码"
                                   autocomplete="off" class="layui-input" id="reg-password" lay-affix="eye">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-wrap">
                            <div class="layui-input-prefix">
                                <i class="layui-icon layui-icon-password"></i>
                            </div>
                            <input type="password" name="confirmPassword" value="" lay-verify="required|confirmPassword"
                                   placeholder="确认新密码" autocomplete="off" class="layui-input" lay-affix="eye">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="submit">重置密码</button>
                    </div>
                </div>
            </div>
        </form>
    </div>
</div>
{/block}

{block name="js"}
<script>
    layui.use(function () {
        var $ = layui.$;
        var form = layui.form;
        var layer = layui.layer;
        var util = layui.util;

        // 提交事件
        form.on('submit(submit)', function (data) {
            var field = data.field; // 获取表单字段值

            var url = "{:url('forget')}";
            $.post(url, field, function (data) {
                if (data.code === 1) {
                    layer.msg(data.msg,{icon:1,time:1500},function () {
                        window.location.href = "{:url('login')}";
                    });
                } else {
                    layer.msg(data.msg)
                }
            }, 'json')

            return false; // 阻止默认 form 跳转
        });
    });

    //发送短信验证码
    function sendSmsCode() {
        var mobile = $('#mobile').val();
        var url = "{:url('smsForgetCode')}";
        $.post(url, {mobile: mobile}, function (data) {
            if (data.code === 1) {
                doLoop(60)
            } else {
                alert(data.msg);
            }
        }, 'json')
    }

    // 倒计时
    function doLoop(seconds) {
        seconds = seconds ? seconds : 60;
        $('#get-vercode').html(seconds + "s后获取");
        $('#get-vercode').attr('disabled', true);
        $('#get-vercode').addClass('layui-btn-disabled');
        let countdown = setInterval(() => {
            if (seconds > 0) {
                $('#get-vercode').html(seconds + "s后获取")
                --seconds;
            } else {
                $('#get-vercode').html("获取验证码");
                $('#get-vercode').attr('disabled', false);
                $('#get-vercode').removeClass('layui-btn-disabled');
                clearInterval(countdown);
            }
        }, 1000);
    }
</script>
{/block}